<?php

namespace App\Http\Controllers\User;

use App\Events\RealName;
use App\Http\Controllers\BaseController;
use App\Models\UserAuth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AuthController extends BaseController
{
    public function show()
    {
        $user_id = auth('api')->id();

        $auth =  UserAuth::query()->where('user_id',$user_id)->first();

        return $this->success($auth);
    }

    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'name' => 'required|max:20',
            'idcard_reverse_photo' => 'required|max:255',
            'idcard_front_photo' => 'required|max:255',
            'sex' => 'required|in:1,2',
        ]);

        $param['user_id'] = auth('api')->id();

        $exists = DB::table('user_auths')->where('user_id',$param['user_id'])->exists();

        if ($exists){
            return  $this->error('您已经提交过了,请勿重复提交');
        }

        $auth = UserAuth::query()->create($param);

        if ($auth->getKey()){
            event(new RealName($param['user_id']));
            return $this->success('提交成功');
        }
        return $this->success('提交失败');

    }
}
